package com.wawei.seeds.me;

import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.Toast;

import com.wawei.seeds.R;
import com.wawei.seeds.adapter.SeedsBaseAdapter;
import com.wawei.seeds.model.WalnutMainEvent;
import com.wawei.seeds.services.ServiceHandler;
import com.wawei.seeds.services.WalnutExpertServiceHandler;
import com.wawei.seeds.utils.NetBase;
import com.wawei.seeds.utils.PrefUtils;
import com.wawei.seeds.utils.RetrofitNetworkService;
import com.wawei.seeds.utils.WalnutBaseNofityHandler;

import butterknife.InjectView;
import retrofit.RestAdapter;

/**
 * Created by itachi on 15/7/20.
 */
public class MeExpertFragment extends MeBaseFragment {
    @InjectView(R.id.walnut_reg_submit)View submitView;

    @InjectView(R.id.walnut_name)EditText nameView;
    @InjectView(R.id.walnut_describe)EditText describeView;
    @InjectView(R.id.walnut_tags)EditText tagsView;

    private MeExpertNotifyHandler notifyHandler;
    private WalnutExpertServiceHandler serviceHandler;
    private static final int REGISTER_EXPERT = 0x01;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        notifyHandler = new MeExpertNotifyHandler(this);
        serviceHandler = new WalnutExpertServiceHandler(getActivity());
    }

    @Override
    public Intent makeIntent4ListItemClicked(AdapterView<?> parent, View view, int position, long id, Object itemData) {
        return null;
    }

    @Override
    public Bundle makeConditionBundle(int phase) {
        return null;
    }

    @Override
    public SeedsBaseAdapter<WalnutMainEvent> makeListAdapter() {
        return null;
    }

    @Override
    public ServiceHandler makeServiceHandler() {
        return null;
    }

    @Override
    public View onCreateViewBeforeInjection(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_walnut_me_expert, container, false);
    }

    @Override
    public void onCreateViewAfterInjection(View view, LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    }

    @Override
    public void onViewCreatedAfterTitle(View view, Bundle savedInstanceState) {
        submitView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String name = nameView.getText().toString();
                String describe = describeView.getText().toString();
                String tags = tagsView.getText().toString();
                Integer userId = PrefUtils.getUserId(getActivity());

                RestAdapter restAdapter = RetrofitNetworkService.makeAsyncRestAdapter();
                serviceHandler.registerExpert(restAdapter, userId, name, describe, tags, notifyHandler, REGISTER_EXPERT);
            }
        });
    }

    @Override
    public String getTitle() {
        return getString(R.string.walnut_me_expert);
    }

    class MeExpertNotifyHandler extends WalnutBaseNofityHandler<MeExpertFragment, NetBase> {

        public MeExpertNotifyHandler(MeExpertFragment meExpertFragment) {
            super(meExpertFragment);
        }

        @Override
        public void handleOtherMessage(Message msg) {
            super.handleOtherMessage(msg);

            Bundle bundle = msg.getData();
            switch (msg.what) {
                case REGISTER_EXPERT:
                    String ok = (String)bundle.get(RetrofitNetworkService.RESULT_DATA);
                    if ("ok".equals(ok)) {
                        Toast.makeText(get().getActivity(), "注册专家成功", Toast.LENGTH_LONG).show();
                    } else  {
                        Toast.makeText(get().getActivity(), "注册专家失败", Toast.LENGTH_LONG).show();
                    }
                    break;
            }
        }
    }
}
